home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / toolbox / poly < prev    next >
Text File  |  1994-09-20  |  2KB  |  72 lines

  1. //-------------------------------------------------------------------//
  2.  
  3. //  Syntax: poly(A)
  4.  
  5. //  Description:
  6.  
  7. //  Find characteristic polynomial of A
  8. //
  9. //  If A is an n by n matrix, poly(A) is an n+1 element row vector
  10. //  whose elements are the coefficients of the characteristic polynomial
  11. //      det(A-zI) = 0.
  12. //  The output coefficients are stored in decending powers as
  13. //      [C1,C2,....,Cn+1]
  14. //  It represents polynomial
  15. //      C[1]*z^n + C[2]*z^(n-1) + .... + C[n]*z + C[n+1]  
  16. //
  17. //  If A is an n by 1 column vector, elements of A are roots of 
  18. //  polynomial
  19. //      (z-A[1])(z-A[2]) ... (z-A[n]) = 0
  20. //  The output coefficients represents the expanded form of above
  21. //  polynomial.
  22.  
  23. //  Example:
  24. //  > X=[5,-6;1,0]
  25. //   X =
  26. //          5         -6
  27. //          1          0
  28. //  > p=poly(X)
  29. //   p =
  30. //          1         -5          6
  31. //  > r=roots(p)
  32. //   r =
  33. //          2
  34. //          3
  35. //  > p1=poly(r)
  36. //   p1 =
  37. //          1         -5          6
  38. //
  39.  
  40. //  See Also: roots
  41. //
  42. //  Tzong-Shuoh Yang (tsyang@ce.berkeley.edu)  5/7/94
  43. //
  44. //-------------------------------------------------------------------//
  45.  
  46. poly = function(A)
  47.     if (A.nc == A.nr) {
  48.       // A is a matrix, find its eigenvalues
  49.       e = eig(A).val';
  50.       l = inf();
  51.       // trim INFs
  52.       e = e[find(e != l && e != -l)];
  53.     else if (A.nr == 1 || A.nc == 1) {
  54.       // elements of A are roots of the polynomial
  55.       e = A[:];    // force a column vector
  56.     else
  57.       error("Argument of poly() must be a square matrix or a vector");
  58.     }}
  59.     // construct char. polynomial
  60.     c = [1,zeros(1,e.nr)];
  61.     for (i in 1:e.nr) {
  62.        j = i + 1;
  63.        c[2:j] = c[2:j] - e[i]*c[1:i];
  64.     }
  65.         
  66.     if (all(imag(c) == 0)) {
  67.        c = real(c);
  68.     }
  69.     return c;
  70. };
  71.